package com.kucai.yifaqapp.presenter.fragment;

import android.widget.Toast;

import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.ResActivity;
import com.kucai.yifaqapp.model.network.entity.res.ResActivitys;
import com.kucai.yifaqapp.model.network.entity.res.ResBanner;
import com.kucai.yifaqapp.model.network.entity.res.ResKaiyuanGame;
import com.kucai.yifaqapp.model.network.entity.res.ResLogin;
import com.kucai.yifaqapp.model.network.entity.res.ResNotices;
import com.kucai.yifaqapp.ui.fragment.LauncherViewPagerHomeFragment;
import com.kucai.yifaqapp.util.SYSOUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LauncherViewPagerHomeFragmentPresenter extends BaseFragmentPresenter<LauncherViewPagerHomeFragment> {


    public LauncherViewPagerHomeFragmentPresenter(LauncherViewPagerHomeFragment mFragment) {
        super(mFragment);


    }

    /**
     * 试玩登录
     */
    public void logindemo() {
        HttpJsonUtils.getHttpApiInstance()
                .demologin(HttpAdress.getuseAdress() + "api/m/guestLogin")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResLogin>(mFragment) {
                    @Override
                    protected void onCustomNext(ResLogin resLogin) {
                        if (resLogin.code == 0) {
                            Info.user = resLogin.data;
                            mFragment.demologin();
                        } else {
                            Toast.makeText(mFragment.getContext(), resLogin.msg, Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }

    //获取首页banner
    public void getbanner() {
        HttpJsonUtils.getHttpApiInstance()
                .getbanner(HttpAdress.getuseAdress() + "api/m/lottery/getHomeLotteryData")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResBanner>(mFragment) {
                    @Override
                    protected void onCustomNext(ResBanner resBanner) {
//                        SYSOUtils.sOut_("首页banner" + resBanner);
                        if (resBanner.code == 0) {
                            mFragment.getBanner(resBanner);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        mFragment.onErrorCustom();
                    }
                });
    }


    //获取活动信息
    public void getactivity() {
        HttpJsonUtils.getHttpApiInstance()
                .getactivity(HttpAdress.getuseAdress() + "api/m/lottery/getSourceData")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResActivity>() {

                    @Override
                    protected void onCustomNext(ResActivity resActivity) {
                        if (resActivity.code == 0 && resActivity.data != null) {
                            mFragment.getActivity(resActivity);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        mFragment.onErrorCustom();
                    }
                });
    }

    //获取登录成功分级弹框
    public void getNotices(String token) {
        HttpJsonUtils.getHttpApiInstance()
                .getNotices(HttpAdress.getuseAdress() + "api/m/user/getNotices", token, "1", "10")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResNotices>() {

                    @Override
                    protected void onCustomNext(ResNotices resActivity) {
                        if (resActivity.code == 0) {
                            mFragment.loginNotices(resActivity);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                    }
                });
    }

    //获取游戏url
    public void getkaiyuangame(String token, String urlid, String gid, String tag) {
        HttpJsonUtils.getHttpApiInstance()
                .getkaiyuangame(HttpAdress.getuseAdress() + "api/GamesApi/action", token, urlid, gid, tag)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResKaiyuanGame>() {

                    @Override
                    protected void onCustomNext(ResKaiyuanGame resKaiyuanGame) {
                        if (resKaiyuanGame.code == 0 && resKaiyuanGame.data != null) {
                            mFragment.getGames(resKaiyuanGame);
                        } else {
                            Toast.makeText(mFragment.getContext(), resKaiyuanGame.msg, Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        Toast.makeText(mFragment.getContext(), "该游戏暂未开放，请联系客服", Toast.LENGTH_LONG).show();
                    }
                });
    }

    //获取游戏url
    public void getactivitys(int id, int imgid) {
        HttpJsonUtils.getHttpApiInstance()
                .getactivitys(HttpAdress.getuseAdress() + "api/m/lottery/getSourceActivty", id, imgid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResActivitys>(mFragment) {

                    @Override
                    protected void onCustomNext(ResActivitys resActivitys) {
                        if (resActivitys.data != null && resActivitys.data.content != null) {
                            mFragment.getActivitys(resActivitys);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
//                        Toast.makeText(mFragment.getContext(), "开元游戏，获取失败", Toast.LENGTH_LONG).show();
                    }
                });
    }

}
